package com.spring.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.springboot.entity.User;
import com.spring.springboot.mapper.UserMapper;
import com.spring.springboot.param.UserParam;
import com.spring.springboot.query.UserQuery;
import com.spring.springboot.result.UserResult;
import com.spring.springboot.service.UserService;
import com.spring.springboot.sys.base.ServiceException;
import com.spring.springboot.sys.core.constant.BusinessConstant;
import com.spring.springboot.sys.core.enums.ResponseCodeEnum;
import com.spring.springboot.sys.util.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户(User)表服务实现类
 *
 * @author 宋义州
 * @since 2022-07-23 01:41:44
 */
@Service
@Transactional(rollbackFor = {Exception.class, RuntimeException.class})
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public void insertSelective(UserParam param) {
        try {
            //校验账号是否重复
            List<User> users = this.baseMapper.selectList(new LambdaQueryWrapper<User>()
                    .eq(User::getDelFlag, BusinessConstant.DeleteStatus.NO_DELETE)
                    .eq(User::getAccount, param.getAccount()));

            if (CollectionUtils.isNotEmpty(users)) {
                throw new ServiceException(500, "该账号已存在！");
            }
            User user = BeanUtils.copyProperties(param, User.class);
            this.baseMapper.insert(user);
        } catch (ServiceException e) {
            throw new ServiceException(ResponseCodeEnum.SERVE_ERROR);
        }
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        try {
            //先查询出该条数据
            User user = this.baseMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getId, id)
                    .eq(User::getDelFlag, BusinessConstant.DeleteStatus.NO_DELETE));
            //标记位改为已删除
            user.setDelFlag(BusinessConstant.DeleteStatus.DELETE);
            this.baseMapper.updateById(user);
        } catch (Exception e) {
            throw new ServiceException(ResponseCodeEnum.SERVE_ERROR);
        }
    }

    @Override
    public void updateByPrimaryKeySelective(UserParam param) {
        try {
            //校验账号是否存在
            User existed = this.baseMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getDelFlag, BusinessConstant.DeleteStatus.NO_DELETE)
                    .eq(User::getAccount, param.getAccount()));
            if (ObjectUtils.isNotEmpty(existed) && ObjectUtils.notEqual(param.getId(), existed.getId())) {
                throw new ServiceException(500, "该账号已存在！");
            }
            User user = BeanUtils.copyProperties(param, User.class);
            this.baseMapper.updateById(user);
        } catch (ServiceException e) {
            throw new ServiceException(ResponseCodeEnum.SERVE_ERROR);
        }
    }

    @Override
    public UserResult selectByPrimaryKey(Long id) {
        try {
            UserResult result = this.baseMapper.selectByPrimaryKey(id);
            if (ObjectUtils.isEmpty(result)) {
                throw new ServiceException(ResponseCodeEnum.RESOURCE_DELETE);
            }
            return result;
        } catch (Exception e) {
            throw new ServiceException(ResponseCodeEnum.SERVE_ERROR);
        }
    }

    @Override
    public Page<UserResult> selectByPage(UserQuery param) {
        try {
            Page<UserResult> page = new Page<>(param.getPageNum(), param.getPageSize());
            page = this.baseMapper.selectByPage(page, param);
            return page;
        } catch (Exception e) {
            throw new ServiceException(ResponseCodeEnum.SERVE_ERROR);
        }
    }

}

